package com.woniu.gf.pawn.productentityweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.gf.pawn.comm.core.domain.*;
import com.woniu.gf.pawn.dto.ProductEntityDto;
import com.woniu.gf.pawn.dto.ProductImageDto;
import com.woniu.gf.pawn.dto.ProductPropertyAndValueDto;
import com.woniu.gf.pawn.dto.ProductPropertyDto;
import com.woniu.gf.pawn.param.PageParam;
import com.woniu.gf.pawn.param.ProductEntityParam;
import com.woniu.gf.pawn.productentityweb.dao.ProductEntityIdentifyImageMapper;
import com.woniu.gf.pawn.productentityweb.dao.ProductEntityMapper;
import com.woniu.gf.pawn.productentityweb.dao.ProductPropertyCentreMapper;
import com.woniu.gf.pawn.productentityweb.service.IProductEntityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TL
 * @since 2020-10-14
 */
@Service
public class ProductEntityServiceImpl extends ServiceImpl<ProductEntityMapper, ProductEntity> implements IProductEntityService {

    @Autowired
    private ProductEntityMapper productEntityMapper;
    @Autowired
    private ProductEntityIdentifyImageMapper imageMapper;
    @Autowired
    private ProductPropertyCentreMapper centreMapper;

    @Override
    public ProductSmallType findSmallTypeName(String query) {

        return productEntityMapper.findSmallTypeName(query);
    }

    @Override
    public ProductBigType findBigTypeName(String query) {
        return productEntityMapper.findBigTypeName(query);
    }

    @Override
    public IPage<ProductEntityDto> findPageProduct(PageParam pageParam) {
        Page<ProductEntityDto> productEntityPage = new Page<>(pageParam.getPageIdx(), pageParam.getPageSize());
        IPage<ProductEntityDto> productEntityDto = productEntityMapper.findPageProduct(productEntityPage,pageParam.getBigTypeId(),pageParam.getEndDate(),pageParam.getManagerId(),pageParam.getProductEntityName(),pageParam.getSmallTypeId(),pageParam.getStartDate(),pageParam.getStatus());

        return productEntityDto;
    }

    @Override
    public Management findManagerId(String manageName) {

        return productEntityMapper.findManagerId(manageName);
    }

    @Override
    public ProductEntityDto findOneShopInfo(Integer productEntityId) {
        return productEntityMapper.findOneShopInfo(productEntityId);
    }

    @Override
    public ProductPropertyAndValueDto findPropertyAndValues(int centreId) {
        return productEntityMapper.findPropertyAndValues(centreId);
    }

    @Override
    public List<ProductImageDto> findImages(Integer productEntityId) {
        return productEntityMapper.findImages(productEntityId);
    }

    @Override
    public void addProductEntity(ProductEntityParam productEntityParam) {
        ProductEntity productEntity = new ProductEntity();
        BeanUtils.copyProperties(productEntityParam,productEntity);
        productEntity.setProductEntityEnterTime(new Date());
        productEntity.setProductEntityStatus("待鉴定");
        productEntityMapper.insert(productEntity);
        int productEntityId=productEntity.getProductEntityId();
        ProductEntityIdentifyImage identifyImage = new ProductEntityIdentifyImage();
        for (ProductImageDto imageDto : productEntityParam.getProductImageDtos()) {
            BeanUtils.copyProperties(imageDto,identifyImage);
            identifyImage.setProductEntityId(productEntityId);
            imageMapper.insert(identifyImage);
    }
        ProductPropertyCentre propertyCentre = new ProductPropertyCentre();
        String centreIds="";
        for (ProductPropertyDto propertyDto : productEntityParam.getProductPropertyDto()) {
            for (ProductPropertyValue propertyValue : propertyDto.getProductPropertyValues()) {
                propertyCentre.setProductId(productEntityParam.getProductId());
                propertyCentre.setProductPropertyId(propertyDto.getProductPropertyId());
                propertyCentre.setProductPropertyValueId(propertyValue.getProductPropertyValueId());
                centreMapper.insert(propertyCentre);
                centreIds+=propertyCentre.getProductPropertyCentreId()+",";
            }
        }
        productEntity.setProductPropertyCenters(centreIds);
        productEntityMapper.updateById(productEntity);
    }

    @Override
    public void updateProductEntity(ProductEntityParam productEntityParam) {
        ProductEntity productEntity=productEntityMapper.selectById(productEntityParam.getProductEntityId());

        int productEntityId=productEntityParam.getProductEntityId();
        ProductEntityIdentifyImage identifyImage = new ProductEntityIdentifyImage();
        for (ProductImageDto imageDto : productEntityParam.getProductImageDtos()) {
            BeanUtils.copyProperties(imageDto,identifyImage);
            identifyImage.setProductEntityId(productEntityId);
            if (identifyImage.getProductEntityIdentifyImageId() == null) {
                imageMapper.insert(identifyImage);
            }
            imageMapper.updateById(identifyImage);
        }

        ProductPropertyCentre propertyCentre = new ProductPropertyCentre();
        String centers = productEntity.getProductPropertyCenters();
        System.out.println(centers);
        if (centers.contains(",")&& StringUtils.isNotEmpty(centers)) {
            String[] split = centers.split(",");
            for (int i = 0; i < split.length; i++) {
                propertyCentre.setProductPropertyCentreId(Integer.parseInt(split[i]));
                centreMapper.deleteById(propertyCentre);
            }
            String centreIds="";
            for (ProductPropertyDto propertyDto : productEntityParam.getProductPropertyDto()) {
                for (ProductPropertyValue propertyValue : propertyDto.getProductPropertyValues()) {
                    propertyCentre.setProductId(productEntityParam.getProductId());
                    propertyCentre.setProductPropertyId(propertyDto.getProductPropertyId());
                    propertyCentre.setProductPropertyValueId(propertyValue.getProductPropertyValueId());
                    centreMapper.insert(propertyCentre);
                    centreIds+=propertyCentre.getProductPropertyCentreId()+",";
                }
            }
            BeanUtils.copyProperties(productEntityParam,productEntity);
            productEntity.setProductPropertyCenters(centreIds);
            productEntityMapper.updateById(productEntity);
        }

    }

    @Override
    public void deleteProductById(Integer productEntityId) {
        ProductEntity productEntity=productEntityMapper.selectById(productEntityId);
        QueryWrapper<ProductEntityIdentifyImage> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("product_entity_id",productEntityId);
        imageMapper.delete(imageQueryWrapper);
        String centers = productEntity.getProductPropertyCenters();
        System.out.println(centers);
        if ( StringUtils.isNotEmpty(centers)) {
            if (centers.contains(",")) {
                String[] split = centers.split(",");
                for (int i = 0; i < split.length; i++) {
                    centreMapper.deleteById(Integer.parseInt(split[i]));
                }
            }
        }
        productEntityMapper.deleteById(productEntityId);

    }
}
